package items;

import main.Map;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import entities.Player;

import shared.Grid;
import shared.MouseInput;
import shared.Timer;

public class Pickaxe extends Tool {
	
	public Pickaxe(int id, int x, int y, int w, int h) throws SlickException {
		super(id, x, y, w, h);
		setTexture("resources/items/pickaxe.png");
	}

	@Override
	public void use(GameContainer gc, Player player) throws SlickException {
		if(gc.getInput().isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
			int midX = player.getX() + player.getWidth()/2;
			int midY = player.getY() + player.getHeight()/2;
			int x = MouseInput.getMousePosX();
			int y = MouseInput.getMousePosY();
			
			/* Field the player can dig/
			 * xxx 1
			 * xxx 2
			 * xpx 3
			 * xxx 4
			 */
			if(getAttackTimer().update() && (Map.getBlock(midX - Grid.getSize(), midY - Grid.getSize()) == Map.getBlock(x, y) ||		//2
					Map.getBlock(midX, midY - Grid.getSize()) == Map.getBlock(x, y) ||
					Map.getBlock(midX + Grid.getSize(), midY - Grid.getSize()) == Map.getBlock(x, y) ||
				Map.getBlock(midX - Grid.getSize(), midY) == Map.getBlock(x, y) ||												//3
					Map.getBlock(midX, midY) == Map.getBlock(x, y) ||
					Map.getBlock(midX + Grid.getSize(), midY) == Map.getBlock(x, y) ||
				Map.getBlock(midX - Grid.getSize(), midY + Grid.getSize()) == Map.getBlock(x, y) ||								//4
					Map.getBlock(midX, midY + Grid.getSize()) == Map.getBlock(x, y) ||
					Map.getBlock(midX + Grid.getSize(), midY + Grid.getSize()) == Map.getBlock(x, y) ||
				Map.getBlock(midX - Grid.getSize(), midY - Grid.getSize()*2) == Map.getBlock(x, y) ||							//1
					Map.getBlock(midX, midY - Grid.getSize()*2) == Map.getBlock(x, y) ||
					Map.getBlock(midX + Grid.getSize(), midY - Grid.getSize()*2) == Map.getBlock(x, y))) {
				if(Map.getBlock(x, y) != null) {
					Map.getBlock(x,y).takeDamage(getAttackDamage());
					Map.getBlock(x,y).checkHealth();
					getAttackTimer().resetTimer();
				}
			}
		}
	}

}
